package com.zzyl.nursing.service.impl;

import java.util.*;

import com.zzyl.common.utils.DateUtils;
import com.zzyl.nursing.domain.NursingPlan;
import com.zzyl.nursing.domain.NursingProject;
import com.zzyl.nursing.domain.vo.NursingLevelVo;
import com.zzyl.nursing.mapper.NursingPlanMapper;
import com.zzyl.nursing.mapper.NursingProjectMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.NursingLevelMapper;
import com.zzyl.nursing.domain.NursingLevel;
import com.zzyl.nursing.service.INursingLevelService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.stream.Collectors;

/**
 * 护理等级Service业务层处理
 *
 * @author ruoyi
 * @date 2025-09-12
 */
@Service
public class NursingLevelServiceImpl extends ServiceImpl<NursingLevelMapper, NursingLevel> implements INursingLevelService
{
    @Autowired
    private NursingLevelMapper nursingLevelMapper;
    @Autowired
    private NursingProjectMapper nursingProjectMapper;
    @Autowired
    private NursingPlanMapper nursingPlanMapper;

    /**
     * 查询护理等级
     *
     * @param id 护理等级主键
     * @return 护理等级
     */
    @Override
    public NursingLevel selectNursingLevelById(Long id)
    {
        return getById(id);
    }

    /**
     * 查询护理等级列表
     *
     * @param nursingLevel 护理等级
     * @return 护理等级
     */
    @Override
    public List<NursingLevelVo> selectNursingLevelList(NursingLevel nursingLevel)
    {
        List<NursingLevel> nursingLevels = nursingLevelMapper.selectNursingLevelList(nursingLevel);
        //这里创建一个vo集合，遍历之前的实体类集合，将所有的内容复制到vo集合中
        List<NursingLevelVo> nursingLevelVos = new ArrayList<>();
        for (NursingLevel level : nursingLevels) {
            NursingLevelVo nursingLevelVo = new NursingLevelVo();
            BeanUtils.copyProperties(level, nursingLevelVo);
            nursingLevelVos.add(nursingLevelVo);
        }
        //按照之前的思路，这里将所有的项目id放到一个set集合中
        Set<Long> lPlanIds = nursingLevels.stream().map(nursingLevel1 -> nursingLevel1.getLplanId())
                .collect(Collectors.toSet());
        //如果这里的项目id集合不为空，使用封装map的方式，将项目id作为key，项目名称作为value
        if (!lPlanIds.isEmpty()) {
            //批量查询lplanid对应项目的名称,拿到所有的项目对象
            List<NursingPlan> nursingPlans = nursingPlanMapper.selectBatchIds(lPlanIds);
            //创建一个map集合，将项目id作为key，项目名称作为value
            Map<Long, String> planMap = nursingPlans.stream().collect(Collectors.toMap(NursingPlan::getId, NursingPlan::getPlanName));
            //遍历vo集合，将vo集合中的项目名称根据Map集合获取补全
            for (NursingLevelVo levelVo : nursingLevelVos) {
                levelVo.setLplanName(planMap.get(levelVo.getLplanId()));
            }
        }
        return nursingLevelVos;
    }

    /**
     * 新增护理等级
     *
     * @param nursingLevel 护理等级
     * @return 结果
     */
    @Override
    public int insertNursingLevel(NursingLevel nursingLevel)
    {
        return save(nursingLevel) ? 1 : 0;
    }

    /**
     * 修改护理等级
     *
     * @param nursingLevel 护理等级
     * @return 结果
     */
    @Override
    public int updateNursingLevel(NursingLevel nursingLevel)
    {
        return updateById(nursingLevel) ? 1 : 0;
    }

    /**
     * 批量删除护理等级
     *
     * @param ids 需要删除的护理等级主键
     * @return 结果
     */
    @Override
    public int deleteNursingLevelByIds(Long[] ids)
    {
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 删除护理等级信息
     *
     * @param id 护理等级主键
     * @return 结果
     */
    @Override
    public int deleteNursingLevelById(Long id)
    {
        return removeById(id) ? 1 : 0;
    }
}